package com.mycompany.System.service;

import com.mycompany.System.entity.receive.TextMsg;
import com.mycompany.System.entity.retur.ArticleMsgRe;
import com.mycompany.System.entity.retur.ArticleRe;
import com.mycompany.System.entity.retur.TextMsgRe;
import com.mycompany.mvc.utils.SystemUtil;
import com.mycompany.mvc.weixinUtils.MsgConstants;
import com.mycompany.mvc.weixinUtils.ParseUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Jintao_Ma on 2017/9/10.
 */
@Service("SystemService")
public class SystemServiceImpl implements SystemService{

    @Override
    public void processRequest(HttpServletRequest request, HttpServletResponse response) {
        String resMsg = SystemUtil.Empty;

        //后面可对其进行简单封装,区别不同的请求消息，以及返回不同的响应消息
        try {
            Map<String,Object> reqMap = ParseUtil.parseXml(request);
            String msgType = MapUtils.getString(reqMap,"MsgType");

            TextMsgRe textMsgRe = new TextMsgRe();

            //针对不同类型请求进行处理
            TextMsg textMsg = new TextMsg();
            if(StringUtils.equals(msgType, MsgConstants.REQ_MESSAGE_TYPE_TEXT)){
                textMsg = ParseUtil.map2Java(textMsg,reqMap);
                BeanUtils.populate(textMsg,reqMap);
                System.out.println(textMsg);
            }else if(StringUtils.equals(msgType, MsgConstants.REQ_MESSAGE_TYPE_IMAGE)){

            }

            List<ArticleRe> articleReList = new ArrayList<>();
            ArticleMsgRe articleMsgRe = new ArticleMsgRe();
            articleMsgRe.setToUserName(MapUtils.getString(reqMap,"FromUserName"));
            articleMsgRe.setFromUserName(MapUtils.getString(reqMap,"ToUserName"));
            articleMsgRe.setCreateTime(new Date().getTime());
            articleMsgRe.setMsgType(MsgConstants.RESP_MESSAGE_TYPE_NEWS);
            articleMsgRe.setFuncFlag(0);

            if(StringUtils.equals("1",MapUtils.getString(reqMap,"Content"))){
                ArticleRe article1 = new ArticleRe();
                article1.setTitle("微信公众帐号开发教程\n引言");
                article1.setDescription("第一个图文");
                article1.setPicUrl("http://www.iteye.com/upload/logo/user/603624/2dc5ec35-073c-35e7-9b88-274d6b39d560.jpg");
                article1.setUrl("http://pic.sogou.com/pics/recompic/detail.jsp?category=%E5%A3%81%E7%BA%B8&tag=%E5%85%A8%E9%83%A8#4%261101427%260");

                ArticleRe article2 = new ArticleRe();
                article2.setTitle("第2篇\n微信公众帐号的类型");
                article2.setDescription("第二个图文");
                article2.setPicUrl("http://www.iteye.com/upload/logo/user/603624/2dc5ec35-073c-35e7-9b88-274d6b39d560.jpg");
                article2.setUrl("http://pic.sogou.com/pics/recompic/detail.jsp?category=%E5%A3%81%E7%BA%B8&tag=%E5%85%A8%E9%83%A8#0%262167036%260");

                ArticleRe article3 = new ArticleRe();
                article3.setTitle("第3篇\n开发模式启用及接口配置");
                article3.setDescription("第三个图文");
                article3.setPicUrl("http://www.iteye.com/upload/logo/user/603624/2dc5ec35-073c-35e7-9b88-274d6b39d560.jpg");
                article3.setUrl("http://pic.sogou.com/pics/recompic/detail.jsp?category=%E5%A3%81%E7%BA%B8&tag=%E5%85%A8%E9%83%A8#0%262167036%260");

                articleReList.add(article1);
                articleReList.add(article2);
                articleReList.add(article3);
                articleMsgRe.setArticleCount(articleReList.size());
                articleMsgRe.setArticles(articleReList);
            }

              // 回复文本消息
//            textMsgRe.setToUserName(MapUtils.getString(reqMap,"FromUserName"));
//            textMsgRe.setFromUserName(MapUtils.getString(reqMap,"ToUserName"));
//            textMsgRe.setCreateTime(new Date().getTime());
//            textMsgRe.setMsgType(MsgConstants.RESP_MESSAGE_TYPE_TEXT);
//            textMsgRe.setFuncFlag(0);
//            textMsgRe.setContent(MapUtils.getString(reqMap,"Content"));

            //回复消息由String转XML类型的字符串
            resMsg = ParseUtil.ArticleMsgReToXml(articleMsgRe);
        } catch (Exception e) {

        }

        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.print(resMsg);
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(null != out){
                out.close();
            }
        }
    }
}
